// 
function MapUtil() 
{
	
    
}

/**
 * 序列化／反序列化 
* 						{
 * 							points:[],	// 二维数组表示多个路径，每个路径是个一维数组，经纬度交错排列，经度在前
 * 							ext:{}		// MapObjExtData
 * 						}
 */
MapUtil.prototype.mapObjToJson = function (mapObj){
	var ret = "{";
	
	ret +=('"points":');
	
	ret += this.mapPathToJson(mapObj.getPath());
	
	ret +=',"ext":';
	var ext = mapObj.getExtData();
	ret +=ext.toJson();
	
	ret +="}";
	return ret;
}
MapUtil.prototype.mapObjFromJson = function (jsonObj){
	
	var ext = new MapObjExtData();
	ext.fromJson(jsonObj.ext);
	
	if(jsonObj.points.length==1){
		var pts = jsonObj.points[0];
		var lines = [];
		var lngLatPtLen = pts.length/2;
		for(var i=0; i<lngLatPtLen; i++){
			lines.push([pts[2*i], pts[2*i+1]]);
		}
		// 如果只有一个子path，则创建AMap.Polyline
		if(ext.type == MapCommon.prototype.OBJECT_TYPE_1 
				|| ext.type == MapCommon.prototype.OBJECT_TYPE_2
				|| ext.type == MapCommon.prototype.OBJECT_TYPE_3){
			// 背景是polygon
			
			var option = MapCommon.prototype.backgroundOptionInstance(ext.type);
			option.path = lines;

			var polyline = new AMap.Polygon(option);
			polyline.setExtData(ext);
			return polyline;
						
		}else{
			var option = MapCommon.prototype.pathOptionInstance(ext.type);
			option.path = lines;

			// 路线是polyline
			var polyline = new AMap.Polyline(option);
			polyline.setExtData(ext);
			return polyline;
		}

	}else{
		// 如果只有多个子path，则创建AMap.Polygon
		alert("未完成");		
	}
	return null;
}
/**
 * 判断 mapObj的path是否由子路径构成
 */
MapUtil.prototype.mapPathToJson = function(pathObj){
	var ret = "[";
	if(pathObj){
		if(this.isMapPathOneChild(pathObj)){ // 只有一个子路径也用二维数组存储
			var len = pathObj.length; //点个数 
			ret +="[";
			for(var i=0;i<len;i++){// 点
				if(i>0){
					ret +=",";
				}
				var pt = pathObj[i];
				ret +=pt.lng;
				ret +=",";
				ret +=pt.lat;
			}
			ret +="]";

		}else{
			var len = pathObj.length; // 子path个数
			for(var i=0;i<len;i++){// 子path
				if(i>0){
					ret +=",";
				}
				var child = pathObj[i];
				var ptLen = child.length; // 子path的点数
				ret +="[";
				for(var j=0;j<ptLen;j++){
					if(j>0){
						ret +=",";
					}
					var pt = child[j];
					ret +=pt.lng;
					ret +=",";
					ret +=pt.lat;
				}
				ret +="]";
				
			}
		}		
	}

	ret +="]";
	return ret;	
}
/**
 * 判断 mapObj的path是否由子路径构成
 */
MapUtil.prototype.isMapPathOneChild = function(pathObj){
	var b  = true;
	if(		(pathObj instanceof Array) 
			&& pathObj.length>0 
			&& (pathObj[0] instanceof Array) ){
		b = false;
	}
	
	return b;
}

